import Vue from 'vue'
import Router from 'vue-router'
import settings from '@/settings'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 * 规范：
 * 二级路由下需要设置meta.activeName字段，取值为二级路由的name，目的是控制激活菜单项的样式
 * 三级路由需要配置meta.activeMenu字段，取值为对应二级路由的path，目的是展开激活的二级菜单
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/cover',
    component: () => import('@/views/cover/index'),
    hidden: true
  },
  {
    path: '/resetpass',
    component: () => import('@/views/resetpass/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/403',
    component: () => import('@/views/403'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: 'home',
        name: 'home', // 这里必须加，否则面包屑组件会报错
        component: () => import('@/views/home/index'),
        meta: { title: '首页', icon: 'dashboard', level: 1, activeName: 'home' },
        noDraw: true,
        children: [
          {
            path: 'manage/:tab?',
            name: 'manage',
            component: () => import('@/views/home/manage'),
            meta: { title: '管理机构', icon: 'dashboard', level: 2, activeName: 'home' },
            hidden: true
          },
          {
            path: 'info/:tab?',
            name: 'info',
            component: () => import('@/views/home/info'),
            meta: { title: '文件和资讯', icon: 'dashboard', level: 2, activeName: 'home' },
            hidden: true
          },
          {
            path: 'announcedetail/:id',
            name: 'announcedetail',
            component: () => import('@/views/home/announcedetail'),
            meta: { title: '公告详情', icon: 'dashboard', level: 2, activeName: 'home' },
            hidden: true
          },
          {
            path: 'summary',
            name: 'summary',
            component: () => import('@/views/home/summary'),
            meta: { title: '学校简介', icon: 'dashboard', level: 2, activeName: 'home' },
            hidden: true
          },
          {
            path: 'pdf',
            name: 'pdf',
            component: () => import('@/views/home/pdf'),
            meta: { title: 'pdf阅读器', icon: 'dashboard', level: 2, activeName: 'home' },
            hidden: true
          }
        ]
      }
    ]
  },
  {
    path: '/dashboard',
    component: Layout,
    redirect: '/dashboard/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard', // 这里必须加，否则面包屑组件会报错
      component: () => import('@/views/dashboard/index'),
      meta: { title: '我的工作台', icon: 'dashboard', level: 1, activeName: 'dashboard' }
    }]
  },
  {
    path: '/basic',
    component: Layout,
    redirect: '/basic/student',
    name: 'basic',
    meta: { title: '基础信息管理', icon: 'example', level: 1, permission: [settings.managePerm] },
    children: [
      {
        path: 'term',
        name: 'term',
        component: () => import('@/views/basic/term/index'),
        meta: { title: '学期管理', icon: 'tree', level: 2, activeName: 'term' }
      },
      {
        path: 'student',
        name: 'student',
        component: () => import('@/views/basic/student/index'),
        meta: { title: '学生管理', icon: 'table', level: 2, activeName: 'student' },
        children: [
          {
            path: 'detail/:id',
            name: 'student-detail',
            hidden: true,
            component: () => import('@/views/basic/student/detail/index'),
            meta: { title: '学生详情', level: 3, activeName: 'student', activeMenu: '/basic/student' }
          },
          {
            path: 'add',
            name: 'student-add',
            hidden: true,
            component: () => import('@/views/basic/student/edit/index'),
            meta: { title: '新增学生', level: 3, activeName: 'student', activeMenu: '/basic/student' }
          },
          {
            path: 'edit/:id',
            name: 'student-edit',
            hidden: true,
            component: () => import('@/views/basic/student/edit/index'),
            meta: { title: '编辑学生', level: 3, activeName: 'student', activeMenu: '/basic/student' }
          },
          {
            path: 'set/:id',
            name: 'student-set',
            hidden: true,
            component: () => import('@/views/basic/student/set/index'),
            meta: { title: '送教设置', level: 3, activeName: 'student', activeMenu: '/basic/student' }
          }
        ]
      },
      {
        path: 'teacher',
        name: 'teacher',
        component: () => import('@/views/basic/teacher/index'),
        meta: { title: '教师管理', icon: 'tree', level: 2, activeName: 'teacher' },
        children: [
          {
            path: 'detail/:id',
            name: 'teacher-detail',
            hidden: true,
            component: () => import('@/views/basic/teacher/detail/index'),
            meta: { title: '教师详情', level: 3, activeName: 'teacher', activeMenu: '/basic/teacher' }
          },
          {
            path: 'add',
            name: 'teacher-add',
            hidden: true,
            component: () => import('@/views/basic/teacher/edit/index'),
            meta: { title: '新增教师', level: 3, activeName: 'teacher', activeMenu: '/basic/teacher' }
          },
          {
            path: 'edit/:id',
            name: 'teacher-edit',
            hidden: true,
            component: () => import('@/views/basic/teacher/edit/index'),
            meta: { title: '编辑教师', level: 3, activeName: 'teacher', activeMenu: '/basic/teacher' }
          }
        ]
      },
      {
        path: 'xschool',
        name: 'xschool',
        component: () => import('@/views/basic/xschool/index'),
        meta: { title: '乡校管理', icon: 'tree', level: 2, activeName: 'xschool' }
      }
    ]
  },
  {
    path: '/task',
    name: 'task',
    component: Layout,
    redirect: '/task/send',
    meta: { title: '送教管理', icon: 'example', level: 1 },
    alwaysShow: true, // 如果不写这个，当children只有一项时，不显示一级菜单
    children: [
      {
        path: 'send',
        name: 'send',
        component: () => import('@/views/task/send/index'),
        meta: { title: '送教任务列表', icon: 'table', level: 2, activeName: 'send' },
        children: [
          {
            path: 'add',
            name: 'send-add',
            hidden: true,
            component: () => import('@/views/task/send/add/index'),
            meta: { title: '新建送教任务', level: 3, activeName: 'send', activeMenu: '/task/send', permission: [settings.managePerm] }
          },
          {
            path: 'edit/:id',
            name: 'send-edit',
            hidden: true,
            component: () => import('@/views/task/send/add/index'),
            meta: { title: '编辑送教任务', level: 3, activeName: 'send', activeMenu: '/task/send', permission: [settings.managePerm] }
          },
          {
            path: 'detail/:id',
            name: 'send-detail',
            hidden: true,
            component: () => import('@/views/task/send/detail/index'),
            meta: { title: '送教任务详情', level: 3, activeName: 'send', activeMenu: '/task/send' }
          }
        ]
      },
      {
        path: 'verify',
        name: 'verify',
        component: () => import('@/views/task/verify/index'),
        meta: { title: '送教提报审核', icon: 'table', level: 2, activeName: 'verify', permission: [settings.managePerm] }
      },
      {
        path: 'static',
        name: 'static',
        component: () => import('@/views/task/static/index'),
        meta: { title: '送教统计报表', icon: 'table', level: 2, activeName: 'static', permission: [settings.managePerm] }
      }
    ]
  },
  {
    path: '/operate',
    name: 'operate',
    component: Layout,
    redirect: '/operate/announce',
    meta: { title: '运营管理', icon: 'example', level: 1, permission: [settings.managePerm] },
    alwaysShow: true, // 如果不写这个，当children只有一项时，不显示一级菜单
    children: [
      {
        path: 'announce',
        name: 'announce',
        component: () => import('@/views/operate/announce/index'),
        meta: { title: '公告管理', icon: 'table', level: 2, activeName: 'announce' },
        children: [
          {
            path: 'add',
            name: 'announce-add',
            hidden: true,
            component: () => import('@/views/operate/announce/add/index'),
            meta: { title: '新建公告', level: 3, activeName: 'announce', activeMenu: '/operate/announce' }
          },
          {
            path: 'edit/:id',
            name: 'announce-edit',
            hidden: true,
            component: () => import('@/views/operate/announce/add/index'),
            meta: { title: '编辑公告', level: 3, activeName: 'announce', activeMenu: '/operate/announce' }
          },
          {
            path: 'detail/:id',
            name: 'announce-detail',
            hidden: true,
            component: () => import('@/views/operate/announce/detail/index'),
            meta: { title: '公告详情', level: 3, activeName: 'announce', activeMenu: '/operate/announce' }
          }
        ]
      },
      {
        path: 'file',
        name: 'file',
        component: () => import('@/views/operate/file/index'),
        meta: { title: '政策文件管理', icon: 'table', level: 2, activeName: 'file' }
      },
      {
        path: 'example',
        name: 'example',
        component: () => import('@/views/operate/example/index'),
        meta: { title: '典型案例管理', icon: 'table', level: 2, activeName: 'example' }
      },
      {
        path: 'bussiness',
        name: 'bussiness',
        component: () => import('@/views/operate/bussiness/index'),
        meta: { title: '业务指导管理', icon: 'table', level: 2, activeName: 'bussiness' }
      },
      {
        path: 'topimg',
        name: 'topimg',
        component: () => import('@/views/operate/topimg/index'),
        meta: { title: '首页图片配置', icon: 'table', level: 2, activeName: 'topimg' }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()
// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
